<?php

namespace app\admins\controller;

use think\Controller;
use think\Db;
use Util\SysDb;

class Json extends Controller
{
    public function navs()
    {
        $this->db = new SysDb();
        //用户角色
        $navs = $this->db->table('navs_role')->lists();
        //一级目录
        $catalog = $this->db->table('navs_catalog')->lists();
        //二级目录
        $children = $this->db->table('children')->lists();

        $t = [];
        $k = [];

        foreach ($navs as $i) {
            foreach ($catalog as $j) {
                $c = [];
                foreach ($children as $x) {
                    if ($j['sid'] == $x['sid']) {
                        array_push($c, $x);
                        $j['children'] = $c;
                    }
                }
                if ($i['oid'] == $j['oid']) {
                    array_push($k, $j);
                    $t[$i['role_name']] = $k;
                } else {
                    $t[$i['role_name']] = null;
                }
            }
        }

        exit(json_encode($t));
    }

    public function informationInformation()
    {
        $page = input('param.page');
        $limit = input('param.limit');
        $val = trim(input('param.key'));

        $this->db = new SysDb();
        //模糊查询
        if ($val == '') {
            //索引
            $index = $this->db->table('data_index')->where(['Did' => 't1_1'])->item();
            //数据
            $data1 = $this->db->table('releaselist')->where(['Did' => 't1_1'])->lists();

            $center = [];
            foreach ($data1 as $d) {
                array_push($center, $d);
                $index['data'] = $center;
            }
            //分页
            $count = $this->db->table('releaselist')->where(['Did' => 't1_1'])->count();
            $tol = ($page - 1) * $limit;

            $list = db('releaselist')->limit($tol, $limit)->select();

            $index['count'] = $count;
            $index['data'] = $list;
        } else {
            //索引
            $index = $this->db->table('data_index')->where(['Did' => 't1_1'])->item();
            //数据
            //$data1 = $this->db->table('releaselist')->where(['Did' => 't1_1'], ['releaseName', 'like', '%'.$val.'%'])->lists();
            $data1 = DB::name('releaselist')->where('releaseName', 'like', '%'.$val.'%')->select();
            //var_dump($data1);

            $center = [];
            foreach ($data1 as $d) {
                array_push($center, $d);
                $index['data'] = $center;
            }
            if (!$data1) {
                $index['msg'] = '暂无此记录';
                $index['code'] = 2;
            }
            //分页
            $count = $this->db->table('releaselist')->where(['Did' => 't1_1'])->count();
            $tol = ($page - 1) * $limit;

            $list = DB::name('releaselist')->where('releaseName', 'like', '%'.$val.'%')->limit($tol, $limit)->select();

            $index['count'] = $count;
            $index['data'] = $list;
        }

        $index_json = json_encode($index);

        echo $index_json;
    }

    public function namelist()
    {
        $user = input('param.user');
        $page = input('param.page');
        $limit = input('param.limit');
        $val = trim(input('param.key'));

        $this->db = new SysDb();
        //模糊查询
        if ($val == '') {
            //索引
            $index = [];
            //数据
            $data1 = $this->db->table('examinelist')->where(['Teacher' => $user,'releaseStatus' => '2'])->lists();

            $center = [];
            foreach ($data1 as $d) {
                array_push($center, $d);
                $index['data'] = $center;
            }
            //分页
            $count = $this->db->table('examinelist')->where(['Teacher' => $user,'releaseStatus' => '2'])->count();
            $tol = ($page - 1) * $limit;

            $list = db('examinelist')->where(['Teacher' => $user,'releaseStatus' => '2'])->limit($tol, $limit)->select();
            $index['code'] = 0;
            $index['count'] = $count;
            $index['data'] = $list;
        } else {
            //索引
            $index = [];
            //数据
            //$data1 = $this->db->table('releaselist')->where(['Did' => 't1_1'], ['releaseName', 'like', '%'.$val.'%'])->lists();
            $data1 = DB::name('examinelist')->where( ['Teacher' => $user,'releaseStatus' => '2'])->where('userHobby', 'like', '%'.$val.'%')->select();
            //var_dump($data1);

            $center = [];
            foreach ($data1 as $d) {
                array_push($center, $d);
                $index['data'] = $center;
            }
            if (!$data1) {
                $index['msg'] = '暂无此记录';
                $index['code'] = 2;
            }
            //分页
            $count = $this->db->table('examinelist')->where(['Teacher' => $user,'releaseStatus' => '2'])->count();
            $tol = ($page - 1) * $limit;

            $list = DB::name('examinelist')->where( ['Teacher' => $user,'releaseStatus' => '2'])->where('userHobby', 'like', '%'.$val.'%')->limit($tol, $limit)->select();

            $index['count'] = $count;
            $index['data'] = $list;
            $index['code'] = 0;
        }

        $index_json = json_encode($index);

        exit ($index_json);
    }

    public function examineList()
    {
        $user = input('param.user');
        $page = input('param.page');
        $limit = input('param.limit');
        $val = trim(input('param.key'));

        $this->db = new SysDb();
        //模糊查询
        if ($val == '') {
            //索引
            $index = [];
            //数据
            $data1 = $this->db->table('examinelist')->where(['Teacher' => $user])->lists();

            $center = [];
            foreach ($data1 as $d) {
                array_push($center, $d);
                $index['data'] = $center;
            }
            //分页
            $count = $this->db->table('examinelist')->where(['Teacher' => $user])->count();
            $tol = ($page - 1) * $limit;

            $list = db('examinelist')->where(['Teacher' => $user])->limit($tol, $limit)->select();
            $index['code'] = 0;
            $index['count'] = $count;
            $index['data'] = $list;
        } else {
            //索引
            $index = [];
            //数据
            //$data1 = $this->db->table('releaselist')->where(['Did' => 't1_1'], ['releaseName', 'like', '%'.$val.'%'])->lists();
            $data1 = DB::name('examinelist')->where( ['Teacher' => $user])->where('userHobby', 'like', '%'.$val.'%')->select();
            //var_dump($data1);

            $center = [];
            foreach ($data1 as $d) {
                array_push($center, $d);
                $index['data'] = $center;
            }
            if (!$data1) {
                $index['msg'] = '暂无此记录';
                $index['code'] = 2;
            }
            //分页
            $count = $this->db->table('examinelist')->where(['Teacher' => $user])->count();
            $tol = ($page - 1) * $limit;

            $list = DB::name('examinelist')->where( ['Teacher' => $user])->where('userHobby', 'like', '%'.$val.'%')->limit($tol, $limit)->select();

            $index['count'] = $count;
            $index['data'] = $list;
            $index['code'] = 0;
        }

        $index_json = json_encode($index);

        exit ($index_json);
    }

    public function releaseList()
    {
        $page = input('param.page');
        $limit = input('param.limit');
        $val = trim(input('param.key'));
        $releaseAuthor = input('param.releaseAuthor');

        $this->db = new SysDb();
        //模糊查询
        if ($val == '') {
            //索引
            $index =[];
            //数据
            $data1 = $this->db->table('releaselist')->where(['releaseAuthor' => $releaseAuthor])->lists();

            $center = [];
            foreach ($data1 as $d) {
                array_push($center, $d);
                $index['data'] = $center;
            }
            //分页
            $count = $this->db->table('releaselist')->where(['releaseAuthor' => $releaseAuthor])->count();
            $tol = ($page - 1) * $limit;

            $list = db('releaselist')->where(['releaseAuthor' => $releaseAuthor])->limit($tol, $limit)->select();
            $index['code'] = 0;
            $index['count'] = $count;
            $index['data'] = $list;
        } else {
            //索引
            $index = [];
            //数据
            //$data1 = $this->db->table('releaselist')->where(['Did' => 't1_1'], ['releaseName', 'like', '%'.$val.'%'])->lists();
            $data1 = DB::name('releaselist')->where(['releaseAuthor' => $releaseAuthor])->where('releaseName', 'like', '%'.$val.'%')->select();
            //var_dump($data1);

            $center = [];
            foreach ($data1 as $d) {
                array_push($center, $d);
                $index['data'] = $center;
            }
            if (!$data1) {
                $index['msg'] = '暂无此记录';
                $index['code'] = 2;
            }
            //分页
            $count = $this->db->table('releaselist')->where(['releaseAuthor' => $releaseAuthor])->count();
            $tol = ($page - 1) * $limit;

            $list = DB::name('releaselist')->where(['releaseAuthor' => $releaseAuthor])->where('releaseName', 'like', '%'.$val.'%')->limit($tol, $limit)->select();
            $index['code'] = 0;
            $index['count'] = $count;
            $index['data'] = $list;
        }

        $index_json = json_encode($index);

        exit ($index_json);
    }
    public function registrationInformationlist()
    {
        $user = input('param.user');
        $page = input('param.page');
        $limit = input('param.limit');
        $val = trim(input('param.key'));

        $this->db = new SysDb();
        //模糊查询
        if ($val == '') {
            //索引
            $index = [];
            //数据
            $data1 = $this->db->table('examinelist')->where(['realName' => $user])->lists();

            $center = [];
            foreach ($data1 as $d) {
                array_push($center, $d);
                $index['data'] = $center;
            }
            //分页
            $count = $this->db->table('examinelist')->where(['realName' => $user])->count();
            $tol = ($page - 1) * $limit;

            $list = db('examinelist')->where(['realName' => $user])->limit($tol, $limit)->select();
            $index['code'] = 0;
            $index['count'] = $count;
            $index['data'] = $list;
        } else {
            //索引
            $index = [];
            //数据
            //$data1 = $this->db->table('releaselist')->where(['Did' => 't1_1'], ['releaseName', 'like', '%'.$val.'%'])->lists();
            $data1 = DB::name('examinelist')->where( ['realName' => $user])->where('userHobby', 'like', '%'.$val.'%')->select();
            //var_dump($data1);

            $center = [];
            foreach ($data1 as $d) {
                array_push($center, $d);
                $index['data'] = $center;
            }
            if (!$data1) {
                $index['msg'] = '暂无此记录';
                $index['code'] = 2;
            }
            //分页
            $count = $this->db->table('examinelist')->where(['realName' => $user])->count();
            $tol = ($page - 1) * $limit;

            $list = DB::name('examinelist')->where( ['realName' => $user])->where('userHobby', 'like', '%'.$val.'%')->limit($tol, $limit)->select();

            $index['count'] = $count;
            $index['data'] = $list;
            $index['code'] = 0;
        }

        $index_json = json_encode($index);

        exit ($index_json);
    }
    public function uploadImg()
    {
        $file = request()->file('file');

        if ($file) {
            $info = $file->move('uploads');
            if ($info) {
                $result['code'] = 0;  //为了照顾到layui富文本编辑器的小脾气 所以此处必须要用0表示成功
                $result['msg'] = '上传成功';
                $result['data']['src'] = 'http://127.0.0.1:8000/uploads/'.str_replace('\\', '/', $info->getSaveName());
                $result['data']['title'] = '';
                echo json_encode($result);
                exit();
            } else {
                $result['code'] = 2;
                $result['msg'] = '上传失败';
                $result['data']['src'] = '';
                $result['data']['title'] = '';
                echo json_encode($result);
                exit();
            }
        } else {
            return false;
        }
    }

    public function swiper()
    {
        $time = time();  //获取当前时间戳
        $date = date('Y-m-d H:i:s', time());  //时间戳转换为日期格式
        // strtotime($date);//日期格式转换为当前的时间戳
        $myjson = Db::table('newslist')->where('newsTime', '<=', $date)->order('newsTime desc')->limit(4)->select();
        $center = [];
        if ($myjson) {
            foreach ($myjson as $d) {
                $da = [];
                $da['id'] = $d['newsId'];
                $da['img'] = $d['newsImg'];
                $da['title'] = $d['newsName'];
                $da['content'] = $d['content'];
                $da['abstract'] = $d['abstract'];
                $da['time'] = $d['newsTime'];
                array_push($center, $da);
                // echo json_encode($d['newsImg']);
            }
            $data['code'] = 0;
            $data['count'] = 4;
            $data['data'] = $center;
            exit(json_encode($data));
        }
    }
}
